﻿#region 注释
/************************************************************************************
 * 
 *日期：2008-09-19
 *作者：张华
 *描述：
 *用户留言信息数据访问类
 * 
 * 
 * 
 *************************************************************************************/
#endregion

#region   命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using SiteCommon;
#endregion
namespace SQLServerDAL
{
    #region    用户留言数据访问类

    /// <summary>
    /// 用户留言类

    /// </summary>
    public class UserMessageDA
    {
        #region  添加用户留言
        /// <summary>
        /// 添加用户留言
        /// </summary>
        /// <param name="userMessage">参数用户留言类型</param>
        /// <returns>000000为成功,其他信息为失败</returns>
        public string UserMessageAdd(UserMessage userMessage)
        {
            int rows;
            SqlParameter outParam = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 50);
            outParam.Direction = ParameterDirection.Output;
            SqlParameter[] para = new SqlParameter[]
            {
              new SqlParameter("@UserId",userMessage.UserId),
              new SqlParameter("@Title",userMessage.Title),
              new SqlParameter("@MessageContent",userMessage.MessageContent),
              new SqlParameter("@GoodsId",userMessage.GoodsId),
              new SqlParameter("@UserIP",userMessage.UserIP),
              new SqlParameter("@ShowFlag",userMessage.ShowFlag),
              new SqlParameter("@BackContent",userMessage.BackContent),
              new SqlParameter("@AddTime",userMessage.AddTime),
              new SqlParameter("@RP_Datetime",userMessage.RP_Datetime),
              new SqlParameter("@AddJF",userMessage.AddJF),
              new SqlParameter("@BYZD1",userMessage.BYZD1),
              new SqlParameter("@BYZD2",userMessage.BYZD2),
              new SqlParameter("@BYZD3",userMessage.BYZD3),
              new SqlParameter("@BYZD4",userMessage.BYZD4),
              outParam
            };
            string ret = SqlManager.MySqlHelper.ExecNonSP("UserMessageAdd", para, out rows);
            if (ret.Equals("000000"))
            {
                return outParam.Value.ToString();
            }
            return ret;
        }
        #endregion

        #region   获取留言信息列表根据商品ID
        /// <summary>
        /// 获取当前页留言信息列表
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="recordCount">共多少记录</param>
        /// <param name="messageList">返回一个list列表</param>
        /// <returns>000000为成功,其他信息则为失败</returns>
        public string GetPagingMessageByGoodsId(int pageIndex, int pageSize, string goodsId, out int recordCount, out List<UserMessage> messageList)
        {
            messageList = null;
            recordCount = 0;
            string tableName = "UserMessage U inner join dbo.COMMODITY_AUTO G on U.GoodsId=G.CA_CODE ";
            string fields = "U.*,G.CA_NAME,G.CA_CODE";
            string condition = "and ShowFlag=1";
            if (goodsId != "")
            {
                condition += " and U.goodsId='" + goodsId+"'";
            }
            string keyfield = "U.MessageId";
            string sort = "desc";
            DataTable dt;
            string ret = SQLServerDAL.SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyfield,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                messageList = new List<UserMessage>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    UserMessage UMessage = new UserMessage();
                    UMessage.UserId = Common.GetString(dr["UserId"]);
                    UMessage.MessageId = Common.GetInt(dr["MessageId"]);
                    UMessage.BackContent = Common.GetString(dr["BackContent"]);
                    UMessage.Title = Common.GetString(dr["Title"]);
                    UMessage.MessageContent = Common.GetString(dr["MessageContent"]);
                    UMessage.UserIP = Common.GetString(dr["UserIP"]);
                    UMessage.ShowFlag = Common.GetInt(dr["ShowFlag"]);

                    if (dr["AddTime"] != null)
                    {
                        UMessage.AddTime = Common.GetDateTime(dr["AddTime"]);
                    }
                    if (dr["RP_Datetime"] != null)
                    {
                        UMessage.RP_Datetime = Common.GetDateTime(dr["RP_Datetime"]);
                    }
                    messageList.Add(UMessage);
                }
            }
            return ret;
        }
        #endregion

        #region   获取当前页用户留言列表信息
        /// <summary>
        /// 获取当前页留言信息列表
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="recordCount">共多少记录</param>
        /// <param name="messageList">返回一个list列表</param>
        /// <returns>000000为成功,其他信息则为失败</returns>
        public string GetPagingMessage(int pageIndex, int pageSize, string userId, out int recordCount, out List<Model.UserMessage> messageList)
        {
            messageList = null;
            recordCount = 0;
            string tableName = "UserMessage AS U";
            string fields = "U.* ";
            string condition = "";
            if (userId != "")
            {
                condition = " U.UserId='" + userId+"' and U.BYZD4=1";//某用户账户的留言
            }
            string keyfield = "U.MessageId";
            string sort = "ASC";
            DataTable dt;
            string ret = SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyfield,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                messageList = new List<Model.UserMessage>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    UserMessage UMessage = new UserMessage();
                    UMessage.MessageId = Common.GetInt(dr["MessageId"]);
                    UMessage.BYZD3 = Common.GetInt(dr["BYZD3"]);
                    UMessage.Title = Common.GetString(dr["Title"]);
                    UMessage.BYZD1 = Common.GetString(dr["BYZD1"]);
                    UMessage.MessageContent = Common.GetString(dr["MessageContent"]);
                    UMessage.AddTime = Common.GetDateTime(dr["AddTime"]);

                    UMessage.RP_Datetime = Common.GetDateTime(dr["RP_Datetime"]);
                    UMessage.BackContent = Common.GetString(dr["BackContent"]);

                    messageList.Add(UMessage);
                }

            }
            return ret;
        }
        #endregion

        //*******************后台留言管理使用的方法*********************************
        #region   获取所有用户留言列表
        /// <summary>
        /// 获取所有用户留言列表
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="recordCount">共多少记录</param>
        /// <param name="messageList">返回一个list列表</param>
        /// <returns>000000为成功,其他信息则为失败</returns>
        public string GetAllMessage(int pageIndex, int pageSize, out int recordCount, out List<Model.UserMessage> messageList)
        {
            messageList = null;
            recordCount = 0;
            string tableName = "UserMessage U left join dbo.COMMODITY_AUTO G on U.GoodsId=G.CA_CODE ";
            string fields = "U.*,G.CA_NAME,G.CA_CODE ";
            string condition = "";
            string keyfield = "U.MessageId";
            string sort = "DESC";
            DataTable dt;
            string ret = SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyfield,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                messageList = new List<Model.UserMessage>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    UserMessage UMessage = new UserMessage();
                    UMessage.BYZD1 = Common.GetString(dr["BYZD1"]);
                    UMessage.MessageId = Common.GetInt(dr["MessageId"]);
                    UMessage.UserId=Common.GetString(dr["UserId"]);
                    UMessage.BackContent = Common.GetString(dr["BackContent"]);
                    UMessage.Title = Common.GetString(dr["Title"]);
                    UMessage.MessageContent = Common.GetString(dr["MessageContent"]);
                    UMessage.AddTime = Common.GetDateTime(dr["AddTime"]);
                    UMessage.GoodsId = Common.GetString(dr["GoodsId"]);
                    UMessage.UserIP = Common.GetString(dr["CA_NAME"]);
                    UMessage.ShowFlag = Common.GetInt(dr["ShowFlag"]);
                    UMessage.Replayuser = Common.GetString(dr["Replayuser"]);
                    UMessage.AddJF = Common.GetDecimal(dr["AddJF"]);
                    UMessage.BYZD2 = Common.GetString(dr["BYZD2"]);
                    messageList.Add(UMessage);
                }

            }
            return ret;
        }
        #endregion

        #region   根据留言Id获取留言信息
        /// <summary>
        ///  根据留言Id获取留言信息
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="myUserMessage"></param>
        /// <returns></returns>
        public string getUserMessageByMessageId(string messageId, out UserMessage myUserMessage)
        {
            myUserMessage = new UserMessage();
            DataSet ds;
            string sql = "select * from  userMessage where messageId=" + SiteCommon.Common.GetInt(messageId);
            string ret = SqlManager.MySqlHelper.ExecDatasetSql(sql, out ds);
            if (ret.Equals("000000"))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    myUserMessage.AddTime = SiteCommon.Common.GetDateTime(dr["AddTime"]);
                    myUserMessage.BackContent = SiteCommon.Common.GetString(dr["BackContent"]);
                    myUserMessage.GoodsId = SiteCommon.Common.GetString(dr["GoodsId"]);
                    myUserMessage.MessageContent = SiteCommon.Common.GetString(dr["MessageContent"]);
                    myUserMessage.MessageId = SiteCommon.Common.GetInt(dr["MessageId"]);
                    myUserMessage.Title = SiteCommon.Common.GetString(dr["Title"]);
                    myUserMessage.UserId = SiteCommon.Common.GetString(dr["UserId"]);
                    myUserMessage.UserIP = SiteCommon.Common.GetString(dr["UserIP"]);
                    myUserMessage.AddJF=SiteCommon.Common.GetInt(dr["AddJF"]);
                    myUserMessage.Replayuser = SiteCommon.Common.GetString(dr["ReplayUser"]);
                }
            }
            return ret;

        }
        #endregion

        #region   根据留言ID更改客服回复内容
        /// <summary>
        /// 根据留言Id更改客服回复内容
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="backContent"></param>
        /// <returns></returns>
        public string UserMessageEdit(int messageId, string backContent, string replayUser, DateTime rp_datetime, decimal addjf)
        {
            string sql = "Update userMessage set BackContent='" + backContent + "', ReplayUser='" + replayUser + "', RP_Datetime='" + rp_datetime + "',AddJF=" + addjf +",BYZD2=1"+ " where MessageId=" + messageId;
            return sql;

        }
        #endregion

        #region   根据留言ID更改显示或者隐藏
        /// <summary>
        /// 根据留言Id更改显示或者隐藏
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="backContent"></param>
        /// <returns></returns>
        public string UserMessageEdit(int messageId, int showflag, out int rows)
        {
            rows = 0;
            string sql = "Update userMessage set ShowFlag=" + showflag + " where MessageId=" + messageId;
            return SqlManager.MySqlHelper.ExecNonSql(sql, out rows);

        }
        #endregion
        //***************************************************************************
    }

    #endregion
}
